import { getAllRecord } from "../../../../utils/api/medicalRecord";
import PatientCaseInfo from '../../../../stores/patientCaseInfo'

// pages/home/home-nav/meidical-record/meidical-record.ts
Component({

  /**
   * 组件的属性列表
   */
  properties: {

  },

  /**
   * 组件的初始数据
   */
  data: {
    userInfo: <PatientBaseEntity> {
      patientUid: '',
      wxUid: '',
    },
    recordInfo: [
      {
        caseId: 123,
        date: '2024-04-28',
        chiefComplaint: '老年性心脏病复诊',
        diagnosis: ['慢性心力衰竭', '血脂异常'],
        solution: ['达格列静片 10mg 28片','达格列静片 10mg 28片']
      },
      {
        caseId: 123,
        date: '2024-04-29',
        chiefComplaint: '老年性心脏病复诊',
        diagnosis: ['慢性心力衰竭', '血脂异常'],
        solution: ['达格列静片 10mg 28片','达格列静片 10mg 28片']
      }
    ],
    showStatus: false,
  },

  /**
   * 组件的方法列表
   */
  methods: {
    updateUserInfo(userInfo:PatientBaseEntity) {
      this.setData({
        userInfo: userInfo,
      })
      console.log('record获取的userInfo',this.data.userInfo);
      // TODO: 自己添加更新echart等等数据逻辑
      let patientUid = userInfo.patientUid as string;
      this.getRecord(patientUid);
      if (this.data.recordInfo.length !== 0) {
        this.setData({
          showStatus: true,
        })
      }
    },

    getRecord(patientUid: string|number) {
      getAllRecord(patientUid).then((res: any) => {
        const records = res.data.map((item: any) => {
          return {
            caseId: item.caseId,
            date: item.medicalTime,
            chiefComplaint: item.chiefComplaint,
            diagnosis: item.diagnosisName.split(",").map((name: any) => name.trim()),
            solution: item.otherTreatment.split(",").map((name: any) => name.trim()) as string,
          };
        });
    
        // 覆盖 recordInfo 数组
        this.setData({
          recordInfo: records
        });
        // console.log('处置是',this.data.recordInfo)
      }).catch((err: any) => {
        console.log('获取病历信息失败', err);
      })
    },

    updateCaseId(event: any) {
      const caseId = event.currentTarget.dataset.caseid as number;
      // console.log('更新的全局caseId是', caseId);
      const data =<CaseInfo> {
        patientUid: this.data.userInfo.patientUid, 
        caseId: caseId,
      }
      PatientCaseInfo.onChangeData(data);
    }
  }
})